﻿using CopyMsgToTxt.Helper;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;

public class WindowHelper
{
    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }

    public static Point GetWindowTopLeftCorner(string processName, string windowTitle)
    {
        IntPtr hWnd = FindWindow(null, windowTitle);
        if (hWnd == IntPtr.Zero)
        {
            hWnd = FindWindow(null, getAnotherTitle(windowTitle));
            if(hWnd == IntPtr.Zero)
            {
                return Point.Empty; // Window not found
            }
        }

        // Check if window belongs to the specified process
        uint processId;
        ProcessInfoHelper.GetWindowThreadProcessId(hWnd, out processId);
        var process = System.Diagnostics.Process.GetProcessById((int)processId);
        if (!process.ProcessName.Contains(processName))
        {
            return Point.Empty; // Window found, but belongs to a different process
        }

        RECT rect;
        if (!GetWindowRect(hWnd, out rect))
        {
            return Point.Empty; // Failed to get window rect
        }

        return new Point(rect.Left, rect.Top);
    }

    public static string getAnotherTitle(string windowTitle)
    {
        switch(windowTitle)
        {
            case "钟长明":
                return "钟长明(zhcm)";
            case "谷苏":
                return "谷苏(gusu)";
            case "王旭":
                return "王旭(wangxu)";
            default:
                return windowTitle;
        }
    }

    //public static Point GetWindowTopLeftCorner(string processName,string windowTitle)
    //{
    //    IntPtr hWndFound = IntPtr.Zero;
    //    EnumWindows((hWnd, lParam) =>
    //    {
    //        uint processId;
    //        GetWindowThreadProcessId(hWnd, out processId);
    //        var process = System.Diagnostics.Process.GetProcessById((int)processId);
    //        if (process.ProcessName == processName)
    //        {
    //            StringBuilder sb = new StringBuilder(256);
    //            GetWindowText(hWnd, sb, sb.Capacity);
    //            if (sb.ToString().Contains(windowTitle))
    //            {
    //                hWndFound = hWnd;
    //                return false; // Stop enumeration after finding a match
    //            }
    //        }
    //        return true; // Continue enumeration
    //    }, IntPtr.Zero);

    //    if (hWndFound == IntPtr.Zero)
    //    {
    //        return Point.Empty; // Window not found
    //    }

    //    RECT rect;
    //    if (!GetWindowRect(hWndFound, out rect))
    //    {
    //        return Point.Empty; // Failed to get window rect
    //    }

    //    return new Point(rect.Left, rect.Top);
    //}
    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);

    private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

}
